package com.ideal.manage.esms.service.project;

import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.project.EquipmentManage;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.project.EquipmentManageRepository;
import com.ideal.manage.esms.repository.project.ProjectManageRepository;
import com.ideal.manage.esms.repository.system.UserRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created By Dai On 2018/4/25
 * Remark: 工程配置 - 对象管理 - 设备管理 Service
 */
@Service
public class EquipmentManageService {
    @Autowired
    private EquipmentManageRepository equipmentManageRepository;
    @Autowired
    private ProjectManageRepository projectManageRepository;
    @Autowired
    private UserRepository userRepository;

    /**
     * 工程配置 -> 对象管理 -> 设备管理 -> 数据表
     *
     * @param pageNum
     * @param request
     * @return
     */
    public PageDto findAll(int pageNum, HttpServletRequest request) {
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        //增加删除标识的过滤
        SpecificationOperator isValid = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(isValid);
        MySpecification<EquipmentManage> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 10, sort);
        Page<EquipmentManage> page = equipmentManageRepository.findAll(mySpecifications, pageable);
        //设置PageDto
        List<EquipmentManage> parameters = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameters);
        pageDto.setTotal(total);
        return pageDto;
    }

    /**
     * 工程配置 -> 对象管理 -> 编辑设备树
     *
     * @param id
     * @return
     */
    public List<String> findIdListByProjectId(String id) {
        return equipmentManageRepository.findIdListByProjectId(id);
    }

    /**
     * 将项目的子节点加入这个这个树里面
     * @param id
     * @param ztreeDtos
     * @return
     */
    public List findAllByProjectId(String id, List ztreeDtos) {
        List<SpecificationOperator> operators = new ArrayList<SpecificationOperator>() {{
            add(new SpecificationOperator("isValid", "1", "EQ"));
            add(new SpecificationOperator("project.id", id, "EQ"));
        }};
        MySpecification<EquipmentManage> mySpecifications = new MySpecification<>(operators);
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        List<EquipmentManage> list = equipmentManageRepository.findAll(mySpecifications, sort);
        list.forEach(equipmentManage -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", equipmentManage.getId());
            if (equipmentManage.getParent() == null){
                jsonObject.put("pId", id);
            } else {
                jsonObject.put("pId", equipmentManage.getParent().getId());
            }
            jsonObject.put("name", equipmentManage.getName());
            jsonObject.put("type", equipmentManage.getEquipmentType());
            jsonObject.put("equipmentType", equipmentManage.getEquipmentType());
            jsonObject.put("communication", equipmentManage.getCommunication()==null?"":equipmentManage.getCommunication());
            jsonObject.put("messageName", equipmentManage.getMessageName()==null?"":equipmentManage.getMessageName());
            ztreeDtos.add(jsonObject);
        });
        return ztreeDtos;
    }

    /**
     * 工程配置 -> 对象管理 -> 编辑设备树 -> 保存重命名的子节点
     * @param parent_id
     * @param project_id
     * @param name
     * @return
     */
    public void saveEquipmentName(String parent_id, String project_id, String name, String type, String id) {
        //获取当前登录用户
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        User user = userRepository.findOne(shiroUser.getId());
        EquipmentManage equipmentManage = equipmentManageRepository.findOne(id);
        if (equipmentManage==null){
           //如果equipmentManage不存在
            equipmentManage = new EquipmentManage();
            equipmentManage.setCreateTime(new Date());
            equipmentManage.setCreateUser(user);
        }
        if (!(type.equals("1")&&type.equals("2"))){ // 当不是变压器和电池包的时候
            equipmentManage.setParent(equipmentManageRepository.findOne(parent_id));
        }
        equipmentManage.setProject(projectManageRepository.findOne(project_id));
        equipmentManage.setName(name);
        equipmentManage.setEquipmentType(type);
        equipmentManage.setUpdateTime(new Date());
        equipmentManageRepository.save(equipmentManage);
    }
}
